use criterion::{black_box, criterion_group, criterion_main, Criterion};
use hash_table::hash_table::{HashTable, HashTablePair};

fn hash_table_new_benchmark(c: &mut Criterion) {
    c.bench_function("hash_table_new", |b| {
        b.iter(|| {
            let hash_func = |key: &u32| -> u64 { *key as u64 };
            let equal_func = |a: &u32, b: &u32| -> bool { a == b };
            let _table: HashTable<u32, u32, _, _> = HashTable::new(hash_func, equal_func, 10);
        });
    });
}

fn hash_table_insert_benchmark(c: &mut Criterion) {
    c.bench_function("hash_table_insert", |b| {
        b.iter(|| {
            let hash_func = |key: &u32| -> u64 { *key as u64 };
            let equal_func = |a: &u32, b: &u32| -> bool { a == b };
            let mut table: HashTable<u32, u32, _, _> = HashTable::new(hash_func, equal_func, 10);
            for i in 0..100 {
                table.insert(i, i);
            }
        });
    });
}

fn hash_table_lookup_benchmark(c: &mut Criterion) {
    c.bench_function("hash_table_lookup", |b| {
        let hash_func = |key: &u32| -> u64 { *key as u64 };
        let equal_func = |a: &u32, b: &u32| -> bool { a == b };
        let mut table: HashTable<u32, u32, _, _> = HashTable::new(hash_func, equal_func, 10);
        for i in 0..100 {
            table.insert(i, i);
        }
        b.iter(|| {
            for i in 0..100 {
                let _ = table.lookup(&i);
            }
        });
    });
}

fn hash_table_remove_benchmark(c: &mut Criterion) {
    c.bench_function("hash_table_remove", |b| {
        let hash_func = |key: &u32| -> u64 { *key as u64 };
        let equal_func = |a: &u32, b: &u32| -> bool { a == b };
        let mut table: HashTable<u32, u32, _, _> = HashTable::new(hash_func, equal_func, 10);
        for i in 0..100 {
            table.insert(i, i);
        }
        b.iter(|| {
            for i in 0..100 {
                table.remove(&i);
            }
        });
    });
}

fn hash_table_num_entries_benchmark(c: &mut Criterion) {
    c.bench_function("hash_table_num_entries", |b| {
        let hash_func = |key: &u32| -> u64 { *key as u64 };
        let equal_func = |a: &u32, b: &u32| -> bool { a == b };
        let mut table: HashTable<u32, u32, _, _> = HashTable::new(hash_func, equal_func, 10);
        for i in 0..100 {
            table.insert(i, i);
        }
        b.iter(|| {
            let _ = table.num_entries();
        });
    });
}

criterion_group!(
    benches,
    hash_table_new_benchmark,
    hash_table_insert_benchmark,
    hash_table_lookup_benchmark,
    hash_table_remove_benchmark,
    hash_table_num_entries_benchmark
);
criterion_main!(benches);